package com.crm.hudingjie.controller;

import java.util.List;

import javax.persistence.OneToMany;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crm.hudingjie.services.DeptServices;
import com.fasterxml.jackson.annotation.JsonIgnore;


@Controller
//@RestController
@RequestMapping("/emp")
public class EmpController {
	@Autowired
	DeptServices ds;
	// 测试JPA方式的数据是否通畅
	/**
	 * 访问地址：http://localhost:8080/emp/add 点击提交之后，post提交，添加员工信息
	 * 
	 * @param model
	 * @return
	 */
	@GetMapping("add")
	public String deptAdd(Model model) {
		model.addAttribute("info", "add方法测试");
		// @GetMapping没有带斜杠/，默认的视图是jsp/add.jsp页面
		return "emp/dept_add";
	}

	@PostMapping("adda")
	public void addDept(@RequestParam("dname") String dname) {
		System.out.println(2222);
		ds.addDept(10002, 101, 301, 401);
		//ds.allbook();
	}
	

	/**
	 * 访问地址：http://localhost:8080/emp/dept/list
	 * 以json格式返回数据。注意请在关系@OneToMany修饰的属性上加入注解@JsonIgnore 防止序列化时进入死循环
	 * 
	 * @return
	 *//*
	@GetMapping("dept/list")
	@ResponseBody
	public List<Dept> getDeptAll() {
		return ds.findBydidBetween(1001, 1100);
	}

	// 测试mybatis方法流程是否通畅
	*//**
	 * 访问地址：http://localhost:8080/emp/detail?eid=2
	 * 通过id查询数据，返回页面。@GetMapping加了/。会将信息默认返回到jsp/emp/detail.jsp页面
	 * 
	 * @param eid
	 * @param model
	 * @return
	 *//*
	@GetMapping("/detail")
	public Model getEmpById(@RequestParam("eid") Integer eid, Model model) {
		System.out.println("getEmpById..........." + eid);
		Emp emp = es.selectById(eid);
		System.out.println("查询完毕。。。。。。。。");
		model.addAttribute("emp", emp);
		// 默认跳转到
		return model;
	}

	*//**
	 * 访问地址：http://localhost:8080/emp/list/1001
	 * 
	 * @param did
	 * @return
	 *//*
	@GetMapping("/list/{did}")
	@ResponseBody
	public List<JSONObject> getList(@PathVariable("did") Integer did) {
		System.out.println("restful风格的参数请求：" + did);
		return es.selectAllByDid(did);
	}

	*//**局部异常。仅限于当前Controller。
	 * 优先级别高于全局
	 * @return
	 *//*
	@ExceptionHandler(Exception.class)
	public ModelAndView exception() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("error");
		mv.addObject("msg", "发生了异常");
		return mv;
	}*/

}
